import 'dart:io';

import 'package:dio/dio.dart';
import 'package:hm_show_flutter/api/api.dart';
import 'package:hm_show_flutter/entity/about_entity.dart';
import 'package:hm_show_flutter/entity/course_list_entity.dart';
import 'package:hm_show_flutter/entity/init_entity.dart';
import 'package:hm_show_flutter/entity/member_entity.dart';
import 'package:hm_show_flutter/entity/register_entity.dart';
import 'package:hm_show_flutter/entity/subscribe_list_entity.dart';
import 'package:hm_show_flutter/entity/upload_avatar_entity.dart';
import 'package:hm_show_flutter/entity/user_info_entity.dart';
import 'package:hm_show_flutter/entity/version_entity.dart';
import 'package:hm_show_flutter/generated/json/base/json_convert_content.dart';
import 'package:hm_show_flutter/utils/http_util.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/login_entity.dart';
import 'package:hm_show_flutter/utils/shared_preferences_util.dart';
import 'package:shared_preferences/shared_preferences.dart';

typedef OnSuccess<T>(T data);

typedef OnFail(String message);

class UserService {
  Future login(String account, String password, OnSuccess onSuccess,
      OnFail onFail) async {
    try {
      Map<String, dynamic> params = new FormData.from({
        "username": account,
        "password": password,
      });
      var response =
          await HttpUtil.instance.post(Api.LOGIN, parameters: params);
      if (response['errno'] == 0) {
        LoginEntity userEntity = LoginEntity.fromJson(response);
        onSuccess(userEntity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }
  Future register(String account, String password, OnSuccess onSuccess,
      OnFail onFail) async {
    try {
      Map<String, dynamic> params = new FormData.from({
        "username": account,
        "password": password,
      });
      var response =
          await HttpUtil.instance.post(Api.REGISTER, parameters: params);
      if (response['errno'] == 0) {
        RegisterEntity userEntity = RegisterEntity.fromJson(response);
        onSuccess(userEntity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future loginOut(OnSuccess onSuccess, OnFail onFail) async {
    try {
      var response = await HttpUtil.instance.post(Api.LOGIN_OUT);
      if (response['errno'] == 0) {
        SharedPreferences sp = await SharedPreferences.getInstance();
        await sp.setString(Strings.token, null);
        await sp.setString(SharedPreferencesUtils.SP_COMPANY, null);
        await sp.setString(SharedPreferencesUtils.SP_USER, null);
        onSuccess("onSuccess");
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getUserInfo(OnSuccess onSuccess, {OnFail onFail}) async {
    try {
      var response = await HttpUtil.instance.get(Api.GET_USER_INFO);
      if (response['errno'] == 0) {
        UserInfoEntity userEntity = UserInfoEntity.fromJson(response);
        onSuccess(userEntity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future uploadAvatar(File img, OnSuccess onSuccess, {OnFail onFail}) async {
    try {
      String path = img.path;
      var name = path.substring(path.lastIndexOf("/") + 1, path.length);
      FormData formData =
          new FormData.from({"file": UploadFileInfo(img, name)});

      var response =
          await HttpUtil.instance.post(Api.UPLOAD_IMG, parameters: formData);
      if (response['errno'] == 0) {
        UploadAvatarEntity entity = UploadAvatarEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future updateUserInfo(OnSuccess onSuccess,
      {String name, String password,String url, String mobile, OnFail onFail}) async {
    try {
      FormData data = new FormData();

      if (name != null) {
        data.add("name", name);
      }
      if (password != null) {
        data.add("password", password);
      }
      if (url != null) {
        data.add("url", url);
      }
      if (mobile != null) {
        data.add("mobile", mobile);
      }

      var response =
          await HttpUtil.instance.post(Api.UPDATE_USER_INFO, parameters: data);
      if (response['errno'] == 0) {
//        UserInfoEntity userEntity = UserInfoEntity.fromJson(response);

      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getAppVersion(OnSuccess onSuccess, {OnFail onFail}) async {
    try {
      FormData data = new FormData();
      data.add("app-type", Platform.isAndroid ? 0 : 1);

      var response =
          await HttpUtil.instance.get(Api.GET_APP_VERSION, parameters: data);
      if (response['errno'] == 0) {
        VersionEntity entity = VersionEntity.fromJson(response);

        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getMemberList(OnSuccess onSuccess, {OnFail onFail}) async {
    try {

      var response =
          await HttpUtil.instance.get(Api.GET_MEMBER_LIST);
      if (response['errno'] == 0) {
        MemberEntity entity = MemberEntity.fromJson(response);
        onSuccess(entity.data);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getCourseListData(int page, OnSuccess onSuccess,
      {OnFail onFail}) async {
    try {
      FormData data = new FormData();

      data.add("pageNumber", page);

      var response =
      await HttpUtil.instance.get(Api.COURSE_LIST, parameters: data);
      if (response['errno'] == 0) {
        CourseListEntity entity = CourseListEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getAppointmentListData(int page, OnSuccess onSuccess,
      {OnFail onFail}) async {
    try {
      FormData data = new FormData();

      data.add("pageNumber", page);

      var response =
      await HttpUtil.instance.get(Api.APPOINTMENT_LIST, parameters: data);
      if (response['errno'] == 0) {
        SubscribeListEntity entity = JsonConvert.fromJsonAsT<SubscribeListEntity>(response);
//        AppointmentListEntity entity = AppointmentListEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getAboutUsData(OnSuccess onSuccess,
      {OnFail onFail}) async {
    try {

      var response =
      await HttpUtil.instance.get(Api.ABOUT_US);
      if (response['errno'] == 0) {
        AboutEntity entity = AboutEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getInitData(OnSuccess onSuccess,
      {OnFail onFail}) async {
    try {

      var response =
      await HttpUtil.instance.get(Api.INIT);
      if (response['errno'] == 0) {
        InitEntity entity = InitEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }
}
